package com.neu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.neu.R;
import com.neu.activity.LsActivity;

/**
 * listview的自定义适配器
 */
public class LsAdapter extends BaseAdapter {

    private String[] strAry;

    private Context ctx;

    /**
     * 构造函数传入数据
     * @param strAry
     * @param ctx
     */
    public LsAdapter(String[] strAry, Context ctx) {
        this.strAry = strAry;
        this.ctx = ctx;
    }


    @Override
    public int getCount() {
        return strAry.length;
    }

    @Override
    public Object getItem(int position) {
        return strAry[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if(convertView==null){//第一次创建
            convertView=LayoutInflater.from(ctx).inflate(R.layout.item_layout,null);
            //实例化
            vh=new ViewHolder();
            //绑定对象
            vh.tvImg=convertView.findViewById(R.id.tvImg);
            //缓存
            convertView.setTag(vh);
        }else{//除了第一次
            vh= (ViewHolder) convertView.getTag();
        }

        //绑定数据
        vh.tvImg.setText(strAry[position]);

        return convertView;
    }

    /**
     * 缓存视图对象
     */
     static class ViewHolder{
       public TextView tvImg;

    }

}
